﻿// -----------------------------------------------------------------------
// <copyright file="OrderBAL.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace eGrocery.Order
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using System.Text;
    using eGrocery.BaseDAL.Model;
    using eGrocery.BaseDAL;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class OrderBAL
    {
        //public List<BaseBAL.BaseBAL.

        public List<ShopsByLocation> GetShopsByLocationId(int id, int page, int pagesize)
        {
            return Global.Context.ShopsByLocations.Where(k => k.LOCATION_ID == id)
                .Skip((page - 1) * pagesize).Take(pagesize).ToList();
        }

        public List<ShopsByLocation> GetShopsBySearch(string searchText, int page, int pagesize)
        {
            var result =
                Global.Context.ShopsByLocations.Where(k =>
                    k.Location.LOCATION_DISPLAY_NAME.ToLower().Contains(searchText)
                    || k.Location.LOCATION_FULL_NAME.ToLower().Contains(searchText)
                    || k.Location.ZIP_CODE.Contains(searchText))
                    .Skip((page - 1) * pagesize).Take(pagesize).ToList();
            return result;
        }

        public List<Product> SearchProduct(string searchText, int page, int pageSize)
        {
            var result =
                Global.Context.Products.Where(k =>
                    k.IsALive ||
                    k.Product_Name.ToLower().Contains(searchText)
                    || k.Product_Desc.ToLower().Contains(searchText))
                    .Select(s => GetProductForView(s)).ToList();
            if (!result.Any() && page == 0)
            {
                var foundCategory = SearchCategory(searchText);
                if (foundCategory.Any())
                {
                    foreach (var item in foundCategory)
                    {
                        AddProductsFromCategory(item, result);
                    }
                }
                else
                {
                    var subCategory = SearchSubCategory(searchText);
                    foreach (var item in subCategory)
                    {
                        AddProductsFromSubCategory(item, result);
                    }
                }
            }
            return result.Skip((page * pageSize)).Take(pageSize).ToList();

        }

        private Product GetProductForView(Product obj)
        {
            return obj;
        }

        public List<Category> SearchCategory(string searchText)
        {
            return Global.Context.Categories.Where(k =>
                k.IsALive != false ||
                k.Category_Name.ToLower().Contains(searchText)).ToList();
        }

        public List<SubCategory> SearchSubCategory(string searchText)
        {
            return Global.Context.SubCategories.Where(k =>
                k.IsLive != false ||
                k.SubCategory_Name.ToLower().Contains(searchText)).ToList();
        }


        private void AddProductsFromCategory(Category categorY, List<Product> product)
        {
            foreach (var item in categorY.SubCategories)
            {
                AddProductsFromSubCategory(item, product);
            }
        }

        private void AddProductsFromSubCategory(SubCategory category, List<Product> product)
        {
            product.AddRange(category.Products.Where(k => k.IsALive).Select(s => GetProductForView(s)));
        }

        private List<Product> GetProductsByCategory(int categoryId, int page, int pageSize)
        {
            var categories =
                Global.Context.Categories.Where(k => k.Category_ID == categoryId).Select(s => s.Category_ID);
            var subcategories =
                Global.Context.SubCategories.Where(k => categories.Contains(k.SubCategory_ID)).Select(s => s.SubCategory_ID);
            var products =
                Global.Context.Products.Where(k => subcategories.Contains(k.Product_SubCategory.Value))
                .Skip((page - 1) * pageSize).Take(pageSize);
            return products.ToList();
        }


    }
}
